#include <stdio.h>
#include <omp.h>

int main() {
    int x;  // 未初始化
    
    printf("Running OpenMP task race condition test...\n");

    #pragma omp parallel
    #pragma omp single
    {
        // 任务1：写x=1（声明修改x）
        #pragma omp task shared(x) depend(out: x)
        {
            x = 1;
            printf("Task 1 (Thread %d): Set x = 1\n", omp_get_thread_num());
        }
        
        // 任务2：写x=2（同样声明修改x）
        #pragma omp task shared(x) depend(out: x)
        {
            x = 2;
            printf("Task 2 (Thread %d): Set x = 2\n", omp_get_thread_num());
        }
        
        // 等待所有任务完成
        #pragma omp taskwait
        
        printf("Final x = %d\n", x);
    }
    return 0;
}